﻿using System;
using System.Configuration;
using System.ServiceModel.Configuration;

namespace Unity.Extensions
{
    /// <summary>
    /// Represents a configuration element that contains a behavior extension 
    /// which enable the user to customize service or endpoint behaviors to include
    /// the container to use when using the <see cref="UnityServiceBehavior"/>.
    /// </summary>
    public class UnityBehaviorExtensionElement : BehaviorExtensionElement
    {
        /// <summary>
        /// Name of the configuration attribute for the container name.
        /// </summary>
        const string ContainerConfigurationPropertyName = "containerName";

        /// <summary>
        /// Gets the type of behavior.
        /// </summary>
        /// <returns>
        /// A <see cref="UnityServiceBehavior"/> type.
        /// </returns>
        public override Type BehaviorType
        {
            get { return typeof(UnityServiceBehavior); }
        }

        /// <summary>
        /// Gets or sets the container name in configuration to use when creating services.
        /// </summary>
        /// <value>The container name in configuration to use when creating services.</value>
        [ConfigurationProperty(ContainerConfigurationPropertyName, IsRequired = true)]
        public string ContainerName
        {
            get { return (string)base[ContainerConfigurationPropertyName]; }
            set { base[ContainerConfigurationPropertyName] = value; }
        }

        /// <summary>
        /// Creates a behavior extension based on the current configuration settings.
        /// </summary>
        /// <returns>
        /// The behavior extension.
        /// </returns>
        protected override object CreateBehavior()
        {
            return new UnityServiceBehavior(ContainerName);
        }
    }
}